package me.boot.mq.rabbitmq;

import com.rabbitmq.client.ConnectionFactory;
import lombok.experimental.UtilityClass;
import me.boot.mq.api.MqProperties;

import java.net.URI;

@UtilityClass
public class RabbitHelper {

    public String DEFAULT_EXCHANGE = "me_exchange";
    public String DEFAULT_VHOST = "me_vhost";
    public String ATTR_EXCHANGE = "exchange";
    public String ATTR_VHOST = "vhost";

    public ConnectionFactory factory(MqProperties properties) {
        URI uri = URI.create("rabbit://" + properties.getBrokers());
        String vhost = properties.getAttrOrDefault(ATTR_VHOST, DEFAULT_VHOST);

        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(uri.getHost());
        factory.setPort(uri.getPort());
        factory.setVirtualHost(vhost);
        factory.setUsername(properties.getUser());
        factory.setPassword(properties.getPassword());
        return factory;
    }
}
